package com.liuguang.gateway.config.security.controller;

import com.liuguang.api.user.UserService;
import com.liuguang.api.gateway.security.dto.UserInfoDTO;
import com.liuguang.common.pojo.CommonResult;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @ClassName: UserController
 * @Description：
 * @Author: wanghr
 * Date: 4:50 PM 2020/9/11
 * Version: 1.0
 */
@Api(value = "用户注册登录模块")
//@ApiModel(description = "用户注册登录模块1")
@RestController
@RequestMapping("/api/user/")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //=================用户名密码形式 适合后台管理系统===========
    @ApiOperation(value = "登录、注册前，获取公钥")
    @RequestMapping("getPubicKey")
    public CommonResult getPubicKey() {
        CommonResult result = userService.getPublicKey();
        return result;
    }

    @ApiOperation(value = "获取验证码：后台生成4位随机验证码")
    @RequestMapping("getVerificationCode")
    public CommonResult getVerificationCode(HttpServletRequest request) {
        CommonResult result = userService.getVerificationCode();
        return result;
    }

    /**
     * 用户注册接口：此种注册方式注册的人均为普通用户，权限 需要系统的操作人员配置
     *
     * @params: [userInfoDTO]
     * @return: com.liuguang.common.pojo.CommonResult
     */
    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "enroll", method = RequestMethod.GET)
    public CommonResult enroll(@Valid UserInfoDTO userInfoDTO, HttpServletRequest request) {
        CommonResult result = userService.userEnroll(userInfoDTO, request);
        return result;
    }

    @ApiOperation(value = "用户登录：用户名、密码登录、验证码登录")
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public CommonResult login(UserInfoDTO userInfoDTO, HttpServletRequest request) {
        CommonResult result = userService.login(userInfoDTO,request);
        return result;
    }

    //=================手机形式 适合登录商城系统==================
    @ApiOperation(value = "获取验证码：手机短信的验证码")
    @RequestMapping("getVerificationCodeByPhone")
    public CommonResult getVerificationCodeByPhone(String phoneNumber) {
        CommonResult result = userService.getVerificationCodeByPhone(phoneNumber);
        return result;
    }

    @ApiOperation(value = "用户登录：通过发送手机验证码登录")
    @RequestMapping(value = "loginByPhone", method = RequestMethod.GET)
    public CommonResult loginByPhone(UserInfoDTO userInfoDTO) {
        CommonResult result = userService.loginByPhone(userInfoDTO);
        return result;
    }


    //    @AnyoneCanAccess
    @ApiOperation(value = "验证token测试接口")
    @RequestMapping("test")
    public CommonResult test() {
//        System.out.println(CurrentUserHolder.getCurrentUser());
        System.out.println("111111");
        userService.test();
        return CommonResult.success("测试成功");
    }


}
